package com.example.aidlclient;

import android.app.Activity;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.example.testing.ipcdemo.ICompute;
import com.example.testing.ipcdemo.ISecurityCenter;

/**
 * Created by tangjiangxu on 9/18/17.
 */

public class BinderPoolActivity extends Activity{
    private static final String TAG = "BinderPoolActivity";

    private ISecurityCenter iSecurityCenter;
    private ICompute iCompute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_binderpool);
        new Thread(new Runnable() {
            @Override
            public void run() {
                doWork();
            }
        }).start();
    }

    private void doWork() {
        BinderPool binderPool = BinderPool.getInstance(this);
        IBinder securityBinder = binderPool.queryBinder(BinderPool.BINDER_SECURITY_CENTER);
        iSecurityCenter = ISecurityCenter.Stub.asInterface(securityBinder);
        Log.e(TAG, "doWork: visit ISecurityCenter");
        String msg= "helloworld-安卓";
        Log.e(TAG, "doWork: content:" + msg);

        try {
            String password = iSecurityCenter.encrypt(msg);
            Log.e(TAG, "doWork: entry:" + password);
            Log.e(TAG, "doWork: decrypt:" + iSecurityCenter.decrypt(password));
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        Log.e(TAG, "doWork: visit ICompute");
        IBinder computeBinder = binderPool.queryBinder(BinderPool.BINDER_COMPUTE);
        iCompute = ICompute.Stub.asInterface(computeBinder);
        try {
            Log.e(TAG, "doWork: 3+5=" + iCompute.add(3, 5));
        } catch (RemoteException e) {
            e.printStackTrace();
        }


    }
}
